package com.baosaas.api.baseInfo.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 读取.properties配置文件的内容至Map中。
 * 
 * @author luzh
 *
 */
public class PropertiesUtil {
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmssSSS");

	/**
	 * 读取.properties配置文件的内容至Map中
	 * 
	 * @param propertiesFile
	 * @return
	 */
	public static Map read(String propertiesFile) {
		ResourceBundle rb = ResourceBundle.getBundle(propertiesFile);
		Map map = new HashMap();
		Enumeration enu = rb.getKeys();
		while (enu.hasMoreElements()) {
			Object obj = enu.nextElement();
			Object objv = rb.getObject(obj.toString());
			map.put(obj, objv);
		}
		return map;
	}

	/**
	 * 
	 * @param deviceName
	 *            设备名
	 * @param plateName
	 *            车牌号
	 * @param plateColor
	 *            车牌颜色
	 * @param bt
	 *            数据
	 */
	public static String saveFileImage(String deviceName, String plateName, String plateColor, byte[] bt) {
		String formatDate = formatter.format(new Date());

		// 生成对应设备当天的文件夹 /MjImage/设备/20170101/ 获取路径
		String filePath = createDeviceFile("MjImage", deviceName, formatDate.substring(0, 8));
		if (!filePath.endsWith(File.separator)) {
			filePath = String.format("%s%s", filePath, File.separator);
		}
		String imageName = String.format("%s%s_%s_%s.jpg", filePath,
				formatDate.substring(9, formatDate.length()), plateName, plateColor);
		// 生成车牌图片
		File file = new File(imageName);
		String imagePath = imageName.substring(imageName.indexOf("\\MjImage"));
		imagePath=imagePath.replaceAll("\\\\", "/");
		FileOutputStream outputStream = null;
		try {
			if (!file.exists()) {
				file.createNewFile();
				System.out.println(String.format("保存文件名称:%s",imagePath));
				System.out.println(String.format("保存路径:%s", file.getAbsolutePath()));
			}
			outputStream = new FileOutputStream(file);
			outputStream.write(bt, 0, bt.length);
			outputStream.close();
			return imagePath;
		} catch (IOException e) {
			e.printStackTrace();
			return "保存图片失败";
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return "保存图片失败";
			}
		}
	}

	public static String createDeviceFile(String fileName, String subName, String dateTime) {
		//确认当前目录
		File root = new File(System.getProperty("user.dir"));
		//设置保存目录在tomcat下的webapps\root目录下，方便url访问图片
		String parentPath = root.getParent()+"\\webapps\\ROOT\\";
		fileName = parentPath + fileName;
		System.out.println(fileName);
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		String path = file.getAbsolutePath();
		File newFile = new File(path + File.separator + subName);
		if (!newFile.exists()) {
			newFile.mkdirs();
		}
		File dateFile = new File(newFile.getAbsolutePath() + File.separator + dateTime);
		if (!dateFile.exists()) {
			dateFile.mkdirs();
		}
		return dateFile.getAbsolutePath();
	}

	private static void createFile(String[] name, int i) {
		i = i--;
		if (i < name.length) {
			File file = new File(name[i]);
			String path = file.getAbsolutePath();
			if (!file.exists()) {
				file.mkdirs();
			}
			createFile(name, i);
		}
	}
}